package com.ykw.parking.controller;


import com.ykw.parking.mapper.OrderMapper;
import com.ykw.parking.mapper.UserMapper;
import com.ykw.parking.pojo1.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class ProductDetailsController {
    @Autowired
    UserMapper userMapper;
    @Autowired
    OrderMapper orderMapper;
    @RequestMapping("/rc")
    @ResponseBody
    public String rc(String pid, HttpSession session){
        String  username = (String)session.getAttribute("username");
        User user = userMapper.queryUserbyname(username);
        RealUser realUser = orderMapper.queryUser(user.getId());
        int rcByUid = orderMapper.getRCByUid(pid);
        if(realUser==null){
            return "请先实名";
        }

        int i = userMapper.QueryRC(pid, user.getId());
        int i1 = userMapper.QueryRCAll(user.getId());
        System.out.println("i1=="+i1);
        if(i>0){
            return "您已经认筹过该车位";
        }
        if(i1>=1){
            return "一个账号只能认筹一个车位";
        }
        if(rcByUid>0){
            return "该车位已经被人认筹";
        }
        userMapper.addRC(pid,user.getId());
        userMapper.UpdateRC(pid,1);
        return "成功认筹";
    }

    @RequestMapping("/getMyRc")
    @ResponseBody
    public List getMyRc(HttpSession session){
        String  username = (String)session.getAttribute("username");
        User user = userMapper.queryUserbyname(username);
        List<RenChou> renChous=new ArrayList<>();
        List<String> rcByPlid = orderMapper.getRCByPlid(user.getId());
        for (String s : rcByPlid) {
            ParkingLot parkingLot = orderMapper.getParkingLot(Integer.valueOf(s));
            Parking parking = orderMapper.getParking(Integer.valueOf(parkingLot.getPid()));
            RenChou renChou = new RenChou(String.valueOf(parkingLot.getId()), user.getId(), String.valueOf(parking.getId()),
                    parking.getCity(), parking.getAddress(), parkingLot.getNumber(),parkingLot.getSale());
            renChous.add(renChou);
        }
        return renChous;
    }

    @RequestMapping("/delRc")
    @ResponseBody
    public String delRc(String parkingid,HttpSession session){
        System.out.println(parkingid);
        String  username = (String)session.getAttribute("username");
        User user = userMapper.queryUserbyname(username);
        orderMapper.delRC(user.getId(),parkingid);
        userMapper.UpdateRC(parkingid,0);
        return "1";
    }

}
